package cool.mylog.mylog2end.bean;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.Fastjson2TypeHandler;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "user", autoResultMap = true)
public class User {
    @TableId
    private Long id;

    private String name;

    private String pswd;

    private String img;

    @TableField(typeHandler = Fastjson2TypeHandler.class)
    private JSONObject info = new JSONObject();

    @TableField(typeHandler = Fastjson2TypeHandler.class)
    private JSONObject setting = new JSONObject();

    private Date createtime;

    private String openidQ;

    private String token;

    public User(String name, String pswd) {
        this.name = name;
        this.pswd = pswd;
    }

    public User(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    /**
     * 根据JSON字符串生成User对象
     * JSON中没有的既设置为null
     *
     * @return Log对象
     */
    public static User getUserByJson(String userJson) {
        JSONObject userJsonObj = JSON.parseObject(userJson);
        return new User(
                userJsonObj.getLong("id"),
                userJsonObj.getString("name"),
                userJsonObj.getString("pswd"),
                userJsonObj.getString("img"),
                userJsonObj.getJSONObject("info"),
                userJsonObj.getJSONObject("setting"),
                userJsonObj.getDate("createtime"),
                userJsonObj.getString("openidQ"),
                ""
        );
    }

}
